package userinfologic

import (
	"context"
	"errors"
	"fmt"

	"go-zero-project-02/rpc_study/user_group/user/internal/svc"
	"go-zero-project-02/rpc_study/user_group/user/user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// UserList rpc接口UserList
func (l *UserListLogic) UserList(in *user.UserListRequest) (*user.UserListResponse, error) {
	page := in.Page
	size := in.PageSize

	var start = (page - 1) * size
	var end = page * size
	fmt.Println("start:", start, "end:", end)
	list := getUserList()
	if start > uint32(len(list)) {
		return nil, errors.New("no more data")
	}
	if end > uint32(len(list)) {
		end = uint32(len(list))
	}
	var responseList []*user.UserInfoResponse = make([]*user.UserInfoResponse, 0)
	for i, u := range list {
		if i >= int(start) && i < int(end) {
			responseList = append(responseList, u)
		}
	}
	return &user.UserListResponse{UserList: responseList}, nil
}

func getUserList() []*user.UserInfoResponse {
	var list = make([]*user.UserInfoResponse, 0)
	for i := 0; i < 1000; i++ {
		response := user.UserInfoResponse{
			UserId:   uint32(i),
			Username: "fox-" + fmt.Sprintf("%d", i),
		}
		list = append(list, &response)
	}
	return list
}
